package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.PlaceOrderM;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.adapter
 * email：893855882@qq.com
 * data：16/9/29
 */
public class ConfirmShopCardAdapter extends RecyclerView.Adapter<ConfirmShopCardAdapter.ShopAdapterHolder> {
    private List<PlaceOrderM.DataBean.OrderInfoBean> list = new ArrayList<>();
    private Context mContext;
    private int index = 0;

    public OnCouponClick onCouponClick;

    public interface OnCouponClick {
        void onCoupon(int index, int position, String order_id);
    }

    public ConfirmShopCardAdapter(Context mContext, int index, List<PlaceOrderM.DataBean.OrderInfoBean> list, OnCouponClick onCouponClick) {
        super();
        this.list = list;
        this.mContext = mContext;
        this.index = index;
        this.onCouponClick = onCouponClick;
    }


    @Override
    public ConfirmShopCardAdapter.ShopAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_confirm_item, parent, false);
        return new ShopAdapterHolder(rootView);
    }


    @Override
    public void onBindViewHolder(final ConfirmShopCardAdapter.ShopAdapterHolder holder, final int position) {
        final PlaceOrderM.DataBean.OrderInfoBean infoBean = list.get(position);
        String goods_thumb = infoBean.getGoods_thumb();
        String goods_name = infoBean.getGoods_name();
        String price = infoBean.getPrice();
        String propertyTags = infoBean.getPropertyTags();
        int due_date = infoBean.getDue_date();
        int num = infoBean.getNum();
        holder.tv_title.setText(goods_name);
        holder.tv_spec.setText(propertyTags);
        holder.tv_money.setText("￥" + price);
        holder.tv_time.setText("保质期 至 " + Tools.SubTime2(due_date + ""));
        holder.tv_num.setText("数量 x " + num);
        String ydx = infoBean.getAllow_ydx();
        String post_order = infoBean.getFree_post_order();//1不满足 2满足包邮
        int allow_ydx = 0;
        int free_post_order = 0;//1不满足 2满足包邮
        if (!Tools.isNull(ydx)) {
            allow_ydx = Integer.parseInt(ydx);
        }

        if (!Tools.isNull(post_order)) {
            free_post_order = Integer.parseInt(post_order);
        }

        switch (free_post_order) {
            case 0:
                holder.tv_is_by.setText("");
                holder.tv_is_by.setVisibility(View.GONE);
                break;
            case 1:
                holder.tv_is_by.setVisibility(View.GONE);
                break;
            case 2:
                holder.tv_is_by.setText("满足包邮条件，联系商家配送");
                holder.tv_is_by.setVisibility(View.VISIBLE);
                break;
        }

        switch (allow_ydx) {
            case 0:
                holder.tv_is_ydx.setText("");
                holder.tv_is_ydx.setVisibility(View.GONE);
                break;
            case 1:
                holder.tv_is_ydx.setText("不能使用运东西，联系商家配送");
                holder.tv_is_ydx.setVisibility(View.VISIBLE);
                break;
            case 2:
                holder.tv_is_ydx.setText("");
                holder.tv_is_ydx.setVisibility(View.GONE);
                break;
        }

        if (!Tools.isNull(goods_thumb)) {
            String path = "";
            if (goods_thumb.startsWith("http")) {
                path = goods_thumb;
            } else {
                path = HttpUtil.IMG_THUMB_BASE_URL + goods_thumb;
            }
            Glide.with(mContext)
                    .load(path)
                    .centerCrop()
                    .placeholder(R.mipmap.loading_4b3)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.iv_banner);
        } else {
            holder.iv_banner.setImageResource(R.mipmap.loading_4b3);
        }

        String yhj = infoBean.getYhj();
        if (!Tools.isNull(yhj)) {
            holder.tv_vname.setText(infoBean.getYhj() + "元");
        } else {
            holder.tv_vname.setText("");
        }
        holder.rl_coupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onCouponClick.onCoupon(index, position, infoBean.getOrder_id() + "");
            }
        });

        holder.et_note.setTag(position);
        holder.et_note.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                PlaceOrderM.DataBean.OrderInfoBean bean = list.get((Integer) holder.et_note.getTag());
                bean.setNote(editable.toString());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ShopAdapterHolder extends RecyclerView.ViewHolder {
        ImageView iv_banner, iv_cb;
        TextView tv_title, tv_spec, tv_is_ydx,tv_is_by, tv_money, tv_time, tv_number, tv_num, tv_vname;
        LinearLayout ll_cb;
        EditText et_note;
        RelativeLayout rl_coupon;

        public ShopAdapterHolder(View view) {
            super(view);
            rl_coupon = (RelativeLayout) view.findViewById(R.id.rl_coupon);
            tv_vname = (TextView) view.findViewById(R.id.tv_vname);
            et_note = (EditText) view.findViewById(R.id.et_note);
            ll_cb = (LinearLayout) view.findViewById(R.id.ll_cb);
            tv_is_by = (TextView) view.findViewById(R.id.tv_is_by);
            iv_cb = (ImageView) view.findViewById(R.id.iv_cb);
            tv_is_ydx = (TextView) view.findViewById(R.id.tv_is_ydx);
            tv_number = (TextView) view.findViewById(R.id.tv_number);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_title = (TextView) view.findViewById(R.id.tv_title);
            tv_spec = (TextView) view.findViewById(R.id.tv_spec);
            tv_money = (TextView) view.findViewById(R.id.tv_money);
            iv_banner = (ImageView) view.findViewById(R.id.iv_banner);
            tv_num = (TextView) view.findViewById(R.id.tv_num);
        }
    }

}
